library PlayerBoard requires /*
    */ TimerUtils /*
    */ Practicals /*
    */ Board /*
    */ Integer /*
    */ 

    struct PlayerBoard extends array
        delegate Board m_board

        boolean m_isShow
        boolean m_isClearing

        player m_player

        static method create takes player p returns thistype
            local thistype this = thistype(GetPlayerId(p))
            if this.m_board > 0 then
                return this
            endif

            set this.m_board = Board.create()
            set this.m_player = p
            set this.m_isShow = false
            set this.m_isClearing = false
            set this.title = ""
            set this.titleColor = 0xFFFF00
            set this.minimized[p] = false
            set this.visible[p] = false
            
            return this
        endmethod

        method destroy takes nothing returns nothing
            call m_board.destroy()
            set m_board = 0
            set m_player = null
        endmethod

        static method getIntegerString takes Integer ind, integer index returns string
            if index == ind.m_arrLength - 1 then
                return I2S(ind[index])
            else
                return Integer.toFixedWidthString(ind[index], ind.m_arrElementSize)
            endif
        endmethod

        static method getTextWidth takes Integer ind, integer index returns real
            local integer n = ind[index]
            local integer j
            if index == ind.m_arrLength - 1 then
                set j = 0
                loop
                    exitwhen n == 0
                    set n = n / 10
                    set j = j + 1
                endloop
                return 0.005 * j
            endif
            return 0.015
        endmethod

        static method getStringValue takes Board board, BoardItem it, integer col, integer row, real value, integer color returns integer
            local Integer ind = Integer.create(R2I(value))
            local integer j
            local integer i

            //将数字按每三个一组拆分并储存到数组
            call ind.toArray(3)

            set j = ind.m_arrLength - 1
            set i = 0
            loop
                exitwhen i > j
                set it = board[i + col][row]
                set it.text = getIntegerString(ind, j - i)
                set it.color = color
                set it.width = getTextWidth(ind, j - i)
                call it.setDisplay(true, false)
                set i = i + 1
            endloop
            call ind.destroy()
            return i + col
        endmethod

        method setBoardVisible takes boolean isVisible returns nothing
            set this.visible[this.m_player] = isVisible
        endmethod

        method setBoardMinimize takes boolean isMini returns nothing
            set this.minimized[this.m_player] = isMini
        endmethod

        private static method anon_83d0c85d takes nothing returns nothing
            local thistype this = ReleaseTimer(GetExpiredTimer())
            set this.m_isClearing = false
        endmethod

        method clearBoard takes nothing returns nothing
            set this.m_isClearing = true
            
            call TimerStart(NewTimerEx(this), 0.13, false, function thistype.anon_83d0c85d)

            set this.title = ""
            set this.titleColor = 0xFFFF00
            call this.clear()
        endmethod

        static method clearAllBoard takes nothing returns nothing
            local integer i
            local player p
            set i = 0
            loop
                exitwhen i >= bj_MAX_PLAYERS
                set p = Player(i)
                if IsPlayerPlaying(p) and IsPlayerControllerIsUser(p) then
                    call thistype.create(p).clearBoard()
                endif
                set i = i + 1
            endloop
            set p = null
        endmethod

    endstruct

endlibrary